<?php

namespace app\store\controller\server;

use app\common\library\wechat\WxPay;
use app\common\model\WxappMchi;
use app\common\service\Order as OrderService;
use app\store\controller\Controller;
use app\store\model\server\Withdraw as WithdrawModel;
use app\store\model\server\User as UserModel;
use app\store\model\User as User;

/**
 * 服务人员提现记录
 * Class Setting
 * @package app\store\controller\apps\dealer
 */
class Withdraw extends Controller
{
    /**
     * 提现记录列表
     * @param int $user_id
     * @param int $apply_status
     * @param int $pay_type
     * @param string $search
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index($user_id = null, $apply_status = -1, $pay_type = -1, $search = '')
    {
        $model = new WithdrawModel;
        return $this->fetch('index', [
            'list' => $model->getList($user_id, $apply_status, $pay_type, $search)
        ]);
    }

    /**
     * 提现审核
     * @param $id
     * @return array
     * @throws \think\exception\DbException
     */
    public function submit($id)
    {
        $user = UserModel::detail($id);

        $data = $this->postData('withdraw');

        if ($user['money'] < $data['money']) return $this->renderError('提现余额不能大于可提现金额');

        $model = new WithdrawModel;

        $res = $this->wechat_pay($id, $data['money']);

        if ($res) {
            $data = array_merge([
                'user_id' => $user['user_id'],
                'real_name' => $user['real_name'],
                'pay_type' => 30,
                'bank_name' => $user['bank_name'],
                'bank_account' => $user['bank_account'],
                'bank_card' => $user['bank_card'],
                'apply_status' => 20,
            ], $this->postData('withdraw')
            );

            if ($model->submit($data)) {
                return $this->renderSuccess('操作成功');
            }
        }

        return $this->renderError($model->getError() ?: '操作失败');
    }

    /**
     * 确认打款
     * @param $id
     * @return array
     * @throws \think\exception\DbException
     */
    public function money($id)
    {
        $model = WithdrawModel::detail($id);
        if ($model->money()) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }

    /**
     * 分销商提现：微信支付企业付款
     * @param $id
     * @return array|bool
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function wechat_pay($id, $money)
    {

        // 微信用户信息
        $user = User::detail($id);

        if (!$user) {
            return false;
        }
        // 生成付款订单号
        $orderNO = OrderService::createOrderNo();
        // 付款描述
        $desc = '工作人员工资发放';
        // 微信支付api：企业付款到零钱
        $wxConfig = WxappMchi::getWxappMachCache($user['wxapp_id']);
        $WxPay = new WxPay($wxConfig);
        // 请求付款api
        if ($WxPay->transfers($orderNO, $user['open_id'], $money, $desc)) {
            // 确认已打款
            return true;
        }

        log_print("转钱失败", $user);
        return false;
    }

}